package com.sskj.one.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.one.R;
import com.sskj.one.component.DaggerUserDataComponent;
import com.sskj.one.util.EditUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class PayPwdActivity extends BaseActivity<PayPwdPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.iv_see)
    ImageView ivSee;
    @BindView(R.id.et_pay_pwd_again)
    EditText etPayPwdAgain;
    @BindView(R.id.iv_see_again)
    ImageView ivSeeAgain;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;
    private String mobile;

    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_pay_pwd;
    }

    @Override
    public PayPwdPresenter getPresenter() {
        return new PayPwdPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        tvTitle.setText("设置资金密码");
        mobile = SPUtil.get(SPConfig.MOBILE, "");
        tvMobile.setText(HideUtil.getPhoneHide(mobile));

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if ("0".equals(userData.getIs_set_tpwd())) {
                    tvTitle.setText("设置资金密码");
                } else if ("1".equals(userData.getIs_set_tpwd())) {
                    tvTitle.setText("修改资金密码");
                }
            }
        });


        ClickUtil.click(300, ivSee, () -> {
            EditUtil.togglePs(etPayPwd, ivSee);
        });
        ClickUtil.click(300, ivSeeAgain, () -> {
            EditUtil.togglePs(etPayPwdAgain, ivSeeAgain);
        });
        ClickUtil.click(300, tvGetCode, () -> {
            if (isEmpty(mobile)) {
                ToastUtils.show("获取用户信息失败");
                return;
            }
            mPresenter.getSMSCode(mobile);
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(etCode)) {
                toastHint(etCode);
                return;
            }
            if (isEmpty(etPayPwd)) {
                toastHint(etPayPwd);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(etPayPwd))) {
                ToastUtils.show("请输入8-12位数字字母组合密码");
                return;
            }
            if (isEmpty(etPayPwdAgain)) {
                toastHint(etPayPwdAgain);
                return;
            }
            if (!getText(etPayPwd).equals(getText(etPayPwdAgain))) {
                ToastUtils.show("两次密码输入不一致");
                return;
            }
            mPresenter.setPayPwd(mobile
                    , getText(etCode)
                    , getText(etPayPwd)
                    , getText(etPayPwdAgain));
        });
    }

    @Override
    public void initData() {

    }

    public void getCodeOK() {
        startTimeDown(tvGetCode);
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, PayPwdActivity.class);
        context.startActivity(intent);
    }

    public void setPayPwdOK() {
        ToastUtils.show("操作成功");
        userViewModel.update();
        finish();
    }
}
